package com.dianzi.certification.controller;

import com.dianzi.certification.dto.SchoolInvestigateDTO;
import com.dianzi.certification.entity.SchoolInvestigate;
import com.dianzi.certification.service.SchoolInvestigateService;
import com.dianzi.certification.utils.JwtUtils;
import com.dianzi.certification.vo.R;
import com.dianzi.certification.vo.SchoolInvestigateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-03-29 23:07
 * @function
 */
@RestController
@Api(tags = {"流程：进校考察"})
public class SchoolInvestigateController {

    @Resource
    private SchoolInvestigateService schoolInvestigateService;

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PostMapping("invest")
    @ApiOperation(value = "新增考察")
    public R<String> addInvest(@RequestBody SchoolInvestigateDTO schoolInvestigateDTO,
                            HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return schoolInvestigateService.addInvest(schoolInvestigateDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("invest")
    @ApiOperation(value = "查看考察")
    public R<List<SchoolInvestigateVO>> getInvest(HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return schoolInvestigateService.getInvest(userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("invest/{investId}")
    @ApiOperation(value = "查看考察详情")
    public R<SchoolInvestigate> getInvestDetail(@PathVariable Integer investId,
                                          HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return schoolInvestigateService.getInvestDetail(investId, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PutMapping("invest")
    @ApiOperation(value = "修改考察")
    public R<SchoolInvestigate> getInvestDetail(@RequestBody SchoolInvestigateDTO schoolInvestigateDTO,
                                                HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return schoolInvestigateService.putInvest(schoolInvestigateDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @GetMapping("admin/invest/{certId}")
    @ApiOperation(value = "查看考察详情(认证专家)")
    public R<SchoolInvestigate> getInvestDetailAdmin(@PathVariable Integer certId) {
        return schoolInvestigateService.getInvestDetailAdmin(certId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @PutMapping("admin/invest")
    @ApiOperation(value = "判定认证是否通过：第三阶段")
    public R<String> putInvestAdmin(@RequestParam @ApiParam("认证id") Integer certId,
                                 @RequestParam @ApiParam("同意-1 驳回-2") Integer status,
                                 @RequestParam(required = false) @ApiParam("若通过写备注，若驳回写理由") String remark) {
        return schoolInvestigateService.putInvestAdmin(certId, status, remark);
    }
}
